package com.FengChaoSystem.servlet;


import com.FengChaoSystem.pojo.Order;
import com.FengChaoSystem.pojo.User;
import com.FengChaoSystem.service.RegisterImpl;
import com.FengChaoSystem.service.RegisterService.RegisterService;
import com.FengChaoSystem.untils.SaltMD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private RegisterImpl service = new RegisterService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String emailAddress = request.getParameter("emailAddress");
        String identity = request.getParameter("identity");

        String checkCode=request.getParameter("checkCode");
        HttpSession session = request.getSession();
        String target =(String) session.getAttribute("checkCode");

        if(!checkCode.equals(target)){
            request.setAttribute("register_msg","验证码错误");
            request.getRequestDispatcher("/register.html").forward(request,response);
            return;
        }
        if (username==null||password==null||emailAddress==null||phone==null){
            request.setAttribute("register_msg","信息要完整，亲");
            request.getRequestDispatcher("/register.html").forward(request,response);
            return;
        }

        User user=new User();
        user.setUserName(username);
        user.setPassWord(password);
        User result = service.hasUser(user);

        if(result==null){
            User temp=new User();
            temp.setUserName(username);
            temp.setPassWord( SaltMD5Util.generateSaltPassword(password));
            temp.setIdentity(Integer.parseInt(identity));
            temp.setEmailAddress(emailAddress);
            temp.setPhone(phone);
            service.registerUser(temp);
            session.setAttribute("userName",username);
            request.setAttribute("register_msg","注册成功,请登录");
            User selectResult = service.selectUserIdByName(temp);
            Order order=new Order();
            order.setToUserId(selectResult.getId());
            order.setReceivePhone(phone);
            service.updateAllMyOrderByPhone(order);

            request.getRequestDispatcher("/chooseImage.html?userName="+username).forward(request,response);
        }else {
            request.setAttribute("register_msg","用户名已存在");
            request.getRequestDispatcher("/register.html").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}